package com.bhs.sansonglogistics.ui.consumptionCoupon;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.CouponDetailBean;
import com.bhs.sansonglogistics.bean.CouponDetailList;
import com.bhs.sansonglogistics.bean.CouponList;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 券使用明细列表
 */
public class CouponDetailListFragment extends BaseFragment implements NetCallBack {


    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private BaseQuickAdapter<CouponDetailBean, BaseViewHolder> mAdapter;
    private int page=1;
    private int couponId;
    private int type;

    public static CouponDetailListFragment newInstance(int type, int couponId) {
        CouponDetailListFragment fragment = new CouponDetailListFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putInt("couponId", couponId);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int initLayout() {
        return R.layout.fragment_coupon_detail_list;
    }

    @Override
    public void initView(View root) {
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            couponId = getArguments().getInt("couponId");
        }
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
        mAdapter = new BaseQuickAdapter<CouponDetailBean, BaseViewHolder>(R.layout.item_coupon_detail) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, CouponDetailBean item) {
                Glide.with(getContext()).load(item.getUser_img()).into((ImageView) helper.getView(R.id.iv_head));
                helper.setText(R.id.tv_nick_name,item.getNickname());
                helper.setText(R.id.tv_phone,item.getPhone());
                helper.setText(R.id.tv_time,String.format("领取时间%s",item.getUpdate_time()));
                helper.setText(R.id.tv_period_validity,String.format("有效期至%s",item.getGetCouponInfo().getDate_end()));
                TextView tvCoupon=helper.getView(R.id.tv_coupon);
                tvCoupon.setText(String.format("%s券",item.getCoupon_title()));
                tvCoupon.setBackgroundResource(type!=3?R.drawable.bg_blue_4:R.drawable.bg_darkgray);
            }
        };
    }

    @Override
    public void initData() {
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        loadData();
    }

    private void loadData() {
        networkRequest(netApi.detailList(page, 10, type, couponId), this, 666);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 666) {
            mRefreshLayout.setRefreshing(false);
            CouponDetailList couponList = new Gson().fromJson(json, CouponDetailList.class);
            if (couponList.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(couponList.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(couponList.getData().getList());
                }
                if (mAdapter.getData().size() >= couponList.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            } else {
                ToastUtil.show(couponList.getMsg());
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}